
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.ProductClassify;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class ClassifyViewBinder
        extends ItemViewBinder<ProductClassify, ClassifyViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    OnEditListener onEditListener;

    boolean isEdit;
    boolean isTopCategory;
    boolean isShowSelect;

    public boolean isShowSelect() {
        return isShowSelect;
    }

    public void setShowSelect(boolean showSelect) {
        isShowSelect = showSelect;
    }

    public boolean isTopCategory() {
        return isTopCategory;
    }

    public void setTopCategory(boolean topCategory) {
        isTopCategory = topCategory;
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public OnEditListener getOnEditListener() {
        return onEditListener;
    }

    public void setOnEditListener(OnEditListener onEditListener) {
        this.onEditListener = onEditListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item_classify, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ProductClassify post) {
        holder.setData(post);

        if (isTopCategory){
            holder.tvCount.setVisibility(View.VISIBLE);
            holder.ivRight.setVisibility(View.VISIBLE);
            holder.tvCount.setText(post.getCounts());
        }else {
            holder.tvCount.setVisibility(View.GONE);
            holder.ivRight.setVisibility(View.GONE);

        }
        String type = post.getType();


        if (isEdit && "2".equals(type)){

            holder.ivEdit.setVisibility(View.VISIBLE);
            holder.ivDelete.setVisibility(View.VISIBLE);

        }else {
            holder.ivEdit.setVisibility(View.GONE);
            holder.ivDelete.setVisibility(View.GONE);
        }
        int position = getPosition(holder);
        holder.ivEdit.setTag(R.id.item_key_position,position);
        holder.ivEdit.setTag(R.id.item_key_status,post);

        holder.ivDelete.setTag(R.id.item_key_position,position);
        holder.ivDelete.setTag(R.id.item_key_status,post);

        holder.itemView.setTag(R.id.item_key_position,position);
        holder.itemView.setTag(R.id.item_key_status,post);

        holder.ivDelete.setOnClickListener(this);
        holder.ivEdit.setOnClickListener(this);
        holder.itemView.setOnClickListener(this);

        if (isShowSelect && "2".equals(type) && !isEdit) {
            if (post.isSelect()) {
                holder.ivSelect.setVisibility(View.VISIBLE);
            } else {
                holder.ivSelect.setVisibility(View.GONE);
            }
        } else {
            holder.ivSelect.setVisibility(View.GONE);
        }

    }



    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

            if (o != null && o instanceof ProductClassify) {

                if (R.id.iv_edit == v.getId()){
                    if (onEditListener != null) {
                        onEditListener.onEdit(1, position, (ProductClassify) o);
                    }
                }else if (R.id.iv_delete == v.getId()){
                    if (onEditListener != null) {
                        onEditListener.onEdit(2, position, (ProductClassify) o);
                    }
                } else {
                    ProductClassify pc = (ProductClassify) o;
                    String type = pc.getType();
                    if (isShowSelect && "2".equals(type) && !isEdit) {
                        pc.setSelect(!pc.isSelect());
                        if (!pc.isSelect()) {
                            getAdapter().notifyDataSetChanged();
                            return;
                        }

                    }else if (isShowSelect && "2".equals(type)){
                        return;
                    }

                    if (onRecyclerViewItemClickListener != null) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }
                }
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivDelete;
        private ImageView ivEdit;
        private ImageView ivSelect;
        private ImageView ivRight;
        private TextView tvTitle;
        private TextView tvCount;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvCount = (TextView) itemView.findViewById(R.id.tv_count);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
            ivEdit = (ImageView) itemView.findViewById(R.id.iv_edit);
            ivRight = (ImageView) itemView.findViewById(R.id.iv_right);
            ivSelect = (ImageView) itemView.findViewById(R.id.iv_select);
        }

        void setData(final ProductClassify post) {
            String name = post.getName();
            tvTitle.setText(name);

        }
    }

    public interface OnEditListener {
        void onEdit(int type, int position, ProductClassify pc);
    }
}
